<?php
namespace Admin\Controller;
use Lib\Api\RoleApi;
use Lib\Api\RoleSysMenuApi;
use Lib\Api\SysMenuApi;
use Think\Controller;
use Think\Model;
class RoleController extends Controller {
    private $modelName = 'Role';

    public function index(){
        $this->display();
    }

    public function tolist() {
        $page_size = $_POST['page_size'];
        $page_num = $_POST['page_num'];
        $keyword = $_POST['keyword'];
        $modelObj = M($this->modelName);

        $map = array();
        if ($keyword <> "")
            $map['name'] = array('like', '%'.$keyword.'%');

        $list = RoleApi::getList($map, $page_num, $page_size);
        $total = RoleApi::getTotal($map);

        $data = pageHelper($page_size, $page_num, $list, $total);
        $this->ajaxReturn($data);
    }

    public function delete() {
        $map['id'] = $_POST['id'];
        RoleApi::delete($map);

        //删除之前的关联关系
        RoleSysMenuApi::delete(array('role_id' => $map['id']));

        $data['code'] = 0;
        $this->ajaxReturn($data);
    }

    public function addoredit() {
        $data = RoleApi::getInfo(array('id' => $_GET['id']));

        $data['rolesysmenu'] = RoleSysMenuApi::getList(array('role_id' => $_GET['id']), 1, 9999);

        $map['page_url'] = array('neq', '');
        $data['sysmenu'] = SysMenuApi::getList($map, 1, 99999);

        //$data['id'] = 1;
        $this->assign('data', $data);
        $this->display();
    }

    public function addoreditdata() {
        $modelObj = M($this->modelName);
        $ajaxdata['code'] = 0;
        $menus = $_POST['menus'];
        $id = $_POST['id'];
        //$ajaxdata['menus'] = $menus;
        if (empty($_POST['id'])) {
            $data = $modelObj->create($_POST);
            //数据校验
            if (empty($data['name'])) {
                $ajaxdata['code'] = -1;
                $ajaxdata['msg'] = '未填写角色名称';
                $this->ajaxReturn($ajaxdata);
            }

            //添加的数据是否存在
            $result = RoleApi::getTotal(array('name' => $data['name']));
            if ($result == 0) {
                //数据不存在，添加数据
                $id = RoleApi::add($data);
                $ajaxdata['msg'] = '添加成功';
            } else {
                $ajaxdata['code'] = -1;
                $ajaxdata['msg'] = '添加失败，角色已存在';
            }
        } else {
            $modelObj = M($this->modelName);
            RoleApi::save($modelObj->create($_POST));
            $ajaxdata['msg'] = '修改成功';
        }

        //角色保存成功，添加权限信息
        if ($ajaxdata['code'] == 0) {
            //删除之前的关联关系
            RoleSysMenuApi::delete(array('role_id' => $id));
            if (!empty($menus)) {
                for ($i= 0;$i< count($menus); $i++){
                    $data2['role_id'] = $id;
                    $data2['menu_id'] = $menus[$i];
                    RoleSysMenuApi::add($data2);
                }
            }
        }

        $this->ajaxReturn($ajaxdata);
    }

    public function getMenu() {
        $map['page_url'] = array('neq', '');
        $list = SysMenuApi::getList($map, 1, 99999);
        $this->ajaxReturn($list);
    }
}